/*
 * @Author: 杨智文 
 * @Date: 2018-11-02 11:19:34 
 * @Last Modified by: william
 * @Last Modified time: 2019-09-26 10:56:27
 */

;
(function (global) {
  if (typeof module !== 'undefined' && typeof exports === 'object') {
    module.exports = new DateUtil();
  } else if (typeof define === 'function' && (define.cmd || define.amd)) {
    define(DateUtil);
  } else {
    global.TD = global.dateUtil = new DateUtil();
  }

})(typeof window !== 'undefined' ? window : global);

function DateUtil() {
  this.Date = new Date()
  this.year = this.Date.getFullYear()
  this.month = this.Date.getMonth() + 1
  this.date = this.Date.getDate()
  this.day = this.Date.getDay()
};

/**
 * 日期对象格式化
 */
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "H+": this.getHours(), //小时 
    "m+": this.getMinutes(), //分 
    "s+": this.getSeconds(), //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
      "00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};

/**
 * @description 获取所在周的星期一,默认当天
 */
DateUtil.prototype.getMonday = function (year, month, date) {
  year = year || this.year
  month = (month ? month : this.month) - 1
  date = date || this.date
  day = new Date(year, month, date).getDay()
  return new Date(year, month, date - day + 1).Format('yyyy-MM-dd')
};

/**
 * @description 获取所在周的星期日,默认当天
 */
DateUtil.prototype.getSunday = function (year, month, date) {
  year = year || this.year
  month = (month ? month : this.month) - 1
  date = date || this.date
  day = new Date(year, month, date).getDay()
  return new Date(year, month, date - day + 7).Format('yyyy-MM-dd')
};

/**
 * @description 获取某年某月第一天,默认当月
 */
DateUtil.prototype.getFirstDay = function (year, month) {
  year = year || this.year
  month = (month ? month : this.month) - 1
  return new Date(year, month, 1).Format('yyyy-MM-dd')
};

/**
 * @description 获取某年某月最后一天,默认当月
 */
DateUtil.prototype.getLastDay = function (year, month) {
  year = year || this.year
  month = month ? month : this.month
  return new Date(year, month, 0).Format('yyyy-MM-dd')
};

/**
 * @description 根据不同得年份和月份判断该月的天数,默认当年当月
 */
DateUtil.prototype.getMonthDays = function (year, month) {
  year = year || this.year
  month = month ? month : this.month
  return new Date(year, month, 0).getDate()
};

/**
 * 时间区间
 * @param {Date} startTime - 开始时间
 * @param {Date} endTime - 结束时间
 * @return {Object} 天-时-分-秒
 */
DateUtil.prototype.interval = function (startTime, endTime) {
  var timestamp = new Date(endTime - startTime).getTime(),
    seconds = timestamp / 1000;
  var dd = parseInt(seconds / 60 / 60 / 24); //计算剩余的天数
  var hh = parseInt(seconds / 60 / 60 % 24); //计算剩余的小时数
  var mm = parseInt(seconds / 60 % 60); //计算剩余的分钟数
  var ss = parseInt(seconds % 60); //计算剩余的秒数
  return {
    dd: dd,
    hh: hh,
    mm: mm,
    ss: ss
  }
};

/**
 * 判断某天是星期几,默认当天
 * @param 时间(年,月,日)
 * @param 前缀,默认星期,可以自定义
 */
DateUtil.prototype.getWeek = function (year, month, date, prefix) {
  year = year || this.year
  month = (month ? month : this.month) - 1
  date = date || this.date
  prefix = prefix || '星期'
  return prefix + "日一二三四五六".charAt(new Date(year, month, date).getDay());
};